﻿using BLL;
using DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Data.Linq;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class admincp_Positions : System.Web.UI.Page
{
    DigitalWorldDataContext db = new DigitalWorldDataContext();
    PositionsBLL p = new PositionsBLL();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.loadData();
            rbtnStatus.Enabled = false;
            btnUpdate.Enabled = false;
        }
    }
    private void loadData()
    {
        gvPositions.DataSource = p.getPosition();
        gvPositions.DataBind();
    }
    protected void btnInsert_Click(object sender, EventArgs e)
    {
        position pst = new position { 
            positionname=txtPositionName.Text,
            description=txtDescription.Text
        };
        p.Position_Insert(pst);
        this.loadData();
        this.reset();
    }
    public void reset() {
        txtPositionName.Text = String.Empty;
        txtDescription.Text = String.Empty;
        rbtnStatus.Enabled = false;
        btnUpdate.Enabled = false;
    }
    protected void btnUpdate_Click(object sender, EventArgs e)
    {
        bool status;
        if (rbtnStatus.SelectedItem.Value == "1")
        {
            status = true;
        }
        else
        {
            status = false;
        }
        position pst = new position{
            positionid = int.Parse(gvPositions.SelectedRow.Cells[0].Text),
            positionname = txtPositionName.Text,
            description = txtDescription.Text,
            status = status
        };
        p.Position_Update(pst);
        this.loadData();
        this.reset();
    }
    protected void gvPositions_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        txtPositionName.Text = Server.HtmlDecode(gvPositions.Rows[e.NewSelectedIndex].Cells[1].Text);
        txtDescription.Text = Server.HtmlDecode(gvPositions.Rows[e.NewSelectedIndex].Cells[2].Text);
        rbtnStatus.Enabled = true;
        btnUpdate.Enabled = true;
    }
    protected void gvPositions_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvPositions.PageIndex = e.NewPageIndex;
        this.loadData();
    }
    protected void gvPositions_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        int positionid = int.Parse(gvPositions.Rows[e.RowIndex].Cells[0].Text);
        p.Position_Delete(positionid);
        this.loadData();
    }
}